package com.lz.sports.binding

import android.view.View
import android.widget.ImageView
import androidx.annotation.DrawableRes
import androidx.databinding.BindingAdapter
import androidx.databinding.InverseBindingAdapter
import androidx.databinding.InverseBindingListener
import com.kyleduo.switchbutton.SwitchButton

object ViewBindings {

    @BindingAdapter("imgSrc")
    @JvmStatic
    fun imageSrc(iv: ImageView, @DrawableRes resId: Int) {
        iv.setImageResource(resId)
    }

    @BindingAdapter("switchChecked")
    @JvmStatic
    fun setSwitchChecked(sv: SwitchButton, switch: Boolean) {
        sv.isChecked = switch
    }

    @BindingAdapter("bgLevel")
    @JvmStatic
    fun setBgLevel(v: View, level: Int) {
        v.background?.level = level
    }

    @BindingAdapter("imageLevel")
    @JvmStatic
    fun setImageLevel(v: ImageView, level: Int) {
        v.setImageLevel(level)
    }

    @InverseBindingAdapter(attribute = "switchChecked", event = "switchCheckedAttrChanged")
    @JvmStatic
    fun getSwitchChecked(sv: SwitchButton): Boolean = sv.isChecked

    @BindingAdapter(value = ["switchCheckedAttrChanged"], requireAll = false)
    @JvmStatic
    fun setSwitchCheckedListener(sv: SwitchButton, attrChange: InverseBindingListener) {
        sv.setOnCheckedChangeListener { _, _ -> attrChange.onChange() }
    }
}